In [2]:
import numpy as np

Scalars


In [3]:
s = np.array(5)

In [4]:
print s


5

In [5]:
s.shape


Out[5]:
()

In [6]:
x = s + 3

In [7]:
print x


8

Vectors


In [8]:
v = np.array([1,2,3])
print v


[1 2 3]

In [9]:
x = v[1]
print x


2

In [11]:
v[1:]


Out[11]:
array([2, 3])

Matrices


In [12]:
m = np.array([[1,2,3], [4,5,6], [7,8,9]])
print m


[[1 2 3]
 [4 5 6]
 [7 8 9]]

In [13]:
m.shape


Out[13]:
(3, 3)

In [14]:
print m[1][2]


6

Tensors


In [16]:
t = np.array([[[[1],[2]],[[3],[4]],[[5],[6]]],[[[7],[8]],\
    [[9],[10]],[[11],[12]]],[[[13],[14]],[[15],[16]],[[17],[17]]]])
print t


[[[[ 1]
   [ 2]]

  [[ 3]
   [ 4]]

  [[ 5]
   [ 6]]]


 [[[ 7]
   [ 8]]

  [[ 9]
   [10]]

  [[11]
   [12]]]


 [[[13]
   [14]]

  [[15]
   [16]]

  [[17]
   [17]]]]

Changing Shapes


In [17]:
v = np.array([1,2,3,4])
print v


[1 2 3 4]

In [18]:
x = v.reshape(1,4)
print x


[[1 2 3 4]]

In [19]:
x = v.reshape(4,1)
print x


[[1]
 [2]
 [3]
 [4]]

In [20]:
x = v.reshape(2,2)
print x


[[1 2]
 [3 4]]

In [21]:
x = v[None, :]
print x


[[1 2 3 4]]

In [22]:
x = v[:, None]
print x


[[1]
 [2]
 [3]
 [4]]

In [23]:
a = np.arange(6)                         # 1d array
print(a)


[0 1 2 3 4 5]

In [24]:
b = np.arange(12).reshape(4,3)           # 2d array
print(b)


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

In [25]:
c = np.arange(24).reshape(2,3,4)         # 3d array
print(c)


[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]

In [27]:
d = np.arange(24*5).reshape(2,3,4,5)         # 4d array
print(d)


[[[[  0   1   2   3   4]
   [  5   6   7   8   9]
   [ 10  11  12  13  14]
   [ 15  16  17  18  19]]

  [[ 20  21  22  23  24]
   [ 25  26  27  28  29]
   [ 30  31  32  33  34]
   [ 35  36  37  38  39]]

  [[ 40  41  42  43  44]
   [ 45  46  47  48  49]
   [ 50  51  52  53  54]
   [ 55  56  57  58  59]]]


 [[[ 60  61  62  63  64]
   [ 65  66  67  68  69]
   [ 70  71  72  73  74]
   [ 75  76  77  78  79]]

  [[ 80  81  82  83  84]
   [ 85  86  87  88  89]
   [ 90  91  92  93  94]
   [ 95  96  97  98  99]]

  [[100 101 102 103 104]
   [105 106 107 108 109]
   [110 111 112 113 114]
   [115 116 117 118 119]]]]

In [33]:
d.sum()


Out[33]:
7140

In [28]:
print(np.arange(10000))


[   0    1    2 ..., 9997 9998 9999]

In [29]:
print(np.arange(10000).reshape(100,100))


[[   0    1    2 ...,   97   98   99]
 [ 100  101  102 ...,  197  198  199]
 [ 200  201  202 ...,  297  298  299]
 ..., 
 [9700 9701 9702 ..., 9797 9798 9799]
 [9800 9801 9802 ..., 9897 9898 9899]
 [9900 9901 9902 ..., 9997 9998 9999]]

In [32]:
a = np.random.random((2,3))
a


Out[32]:
array([[ 0.98871133,  0.66927443,  0.67758828],
       [ 0.89493095,  0.35025139,  0.6696382 ]])

Element-wise Operations


In [37]:
values = [1,2,3,4,5]
print values
for i in range(len(values)):
    values[i] += 5
    
print len(values)
print values


[1, 2, 3, 4, 5]
5
[6, 7, 8, 9, 10]

The NumPy way


In [38]:
values = [1,2,3,4,5]
values = np.array(values) + 5
print values


[ 6  7  8  9 10]

In [39]:
values += 5
print values


[11 12 13 14 15]

In [43]:
x = np.multiply(values,2)
print x

x = values * 3
print x


[22 24 26 28 30]
[33 36 39 42 45]

In [44]:
x *= 0
print x


[0 0 0 0 0]

Element-wise Matrix Operations


In [46]:
a = np.array([[1,3],[5,7]])
print a
# displays the following result:
# array([[1, 3],
#        [5, 7]])

b = np.array([[2,4],[6,8]])
print b
# displays the following result:
# array([[2, 4],
#        [6, 8]])

a + b
# displays the following result
#      array([[ 3,  7],
#             [11, 15]])


[[1 3]
 [5 7]]
[[2 4]
 [6 8]]
Out[46]:
array([[ 3,  7],
       [11, 15]])

NumPy Matrix Multiplication

Element-wise Multiplication


In [47]:
m = np.array([[1,2,3],[4,5,6]])
m
# displays the following result:
# array([[1, 2, 3],
#        [4, 5, 6]])


Out[47]:
array([[1, 2, 3],
       [4, 5, 6]])

In [48]:
n = m * 0.25
n
# displays the following result:
# array([[ 0.25,  0.5 ,  0.75],
#        [ 1.  ,  1.25,  1.5 ]])


Out[48]:
array([[ 0.25,  0.5 ,  0.75],
       [ 1.  ,  1.25,  1.5 ]])

In [49]:
m * n
# displays the following result:
# array([[ 0.25,  1.  ,  2.25],
#        [ 4.  ,  6.25,  9.  ]])


Out[49]:
array([[ 0.25,  1.  ,  2.25],
       [ 4.  ,  6.25,  9.  ]])

In [50]:
np.multiply(m, n)   # equivalent to m * n
# displays the following result:
# array([[ 0.25,  1.  ,  2.25],
#        [ 4.  ,  6.25,  9.  ]])


Out[50]:
array([[ 0.25,  1.  ,  2.25],
       [ 4.  ,  6.25,  9.  ]])

Matrix Product


In [51]:
a = np.array([[1,2,3,4],[5,6,7,8]])
a
# displays the following result:
# array([[1, 2, 3, 4],
#        [5, 6, 7, 8]])


Out[51]:
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

In [52]:
a.shape
# displays the following result:
# (2, 4)


Out[52]:
(2, 4)

In [53]:
b = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
b
# displays the following result:
# array([[ 1,  2,  3],
#        [ 4,  5,  6],
#        [ 7,  8,  9],
#        [10, 11, 12]])


Out[53]:
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])

In [54]:
b.shape
# displays the following result:
# (4, 3)


Out[54]:
(4, 3)

In [55]:
c = np.matmul(a, b)
c
# displays the following result:
# array([[ 70,  80,  90],
#        [158, 184, 210]])


Out[55]:
array([[ 70,  80,  90],
       [158, 184, 210]])

In [56]:
c.shape
# displays the following result:
# (2, 3)


Out[56]:
(2, 3)

NumPy's dot function


In [57]:
a = np.array([[1,2],[3,4]])
a
# displays the following result:
# array([[1, 2],
#        [3, 4]])


Out[57]:
array([[1, 2],
       [3, 4]])

In [58]:
np.dot(a,a)
# displays the following result:
# array([[ 7, 10],
#        [15, 22]])


Out[58]:
array([[ 7, 10],
       [15, 22]])

In [59]:
a.dot(a)  # you can call `dot` directly on the `ndarray`
# displays the following result:
# array([[ 7, 10],
#        [15, 22]])


Out[59]:
array([[ 7, 10],
       [15, 22]])

In [60]:
np.matmul(a,a)
# array([[ 7, 10],
#        [15, 22]])


Out[60]:
array([[ 7, 10],
       [15, 22]])

Transposes in NumPy

Transpose


In [61]:
m = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
m
# displays the following result:
# array([[ 1,  2,  3,  4],
#        [ 5,  6,  7,  8],
#        [ 9, 10, 11, 12]])


Out[61]:
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

In [62]:
m.T
# displays the following result:
# array([[ 1,  5,  9],
#        [ 2,  6, 10],
#        [ 3,  7, 11],
#        [ 4,  8, 12]])


Out[62]:
array([[ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11],
       [ 4,  8, 12]])

In [63]:
m_t = m.T
m_t[3][1] = 200
m_t
# displays the following result:
# array([[ 1,   5, 9],
#        [ 2,   6, 10],
#        [ 3,   7, 11],
#        [ 4, 200, 12]])


Out[63]:
array([[  1,   5,   9],
       [  2,   6,  10],
       [  3,   7,  11],
       [  4, 200,  12]])

In [64]:
m
# displays the following result:
# array([[ 1,  2,  3,   4],
#        [ 5,  6,  7, 200],
#        [ 9, 10, 11,  12]])


Out[64]:
array([[  1,   2,   3,   4],
       [  5,   6,   7, 200],
       [  9,  10,  11,  12]])

A real use case


In [65]:
inputs = np.array([[-0.27,  0.45,  0.64, 0.31]])
inputs
# displays the following result:
# array([[-0.27,  0.45,  0.64,  0.31]])


Out[65]:
array([[-0.27,  0.45,  0.64,  0.31]])

In [66]:
inputs.shape
# displays the following result:
# (1, 4)


Out[66]:
(1, 4)

In [67]:
weights = np.array([[0.02, 0.001, -0.03, 0.036], \
    [0.04, -0.003, 0.025, 0.009], [0.012, -0.045, 0.28, -0.067]])

In [68]:
weights
# displays the following result:
# array([[ 0.02 ,  0.001, -0.03 ,  0.036],
#        [ 0.04 , -0.003,  0.025,  0.009],
#        [ 0.012, -0.045,  0.28 , -0.067]])


Out[68]:
array([[ 0.02 ,  0.001, -0.03 ,  0.036],
       [ 0.04 , -0.003,  0.025,  0.009],
       [ 0.012, -0.045,  0.28 , -0.067]])

In [ ]:
weights.shape
# displays the following result:
# (3, 4)

In [70]:
np.matmul(inputs, weights.T)
# displays the following result:
# array([[-0.01299,  0.00664,  0.13494]])


Out[70]:
array([[-0.01299,  0.00664,  0.13494]])

In [71]:
np.matmul(weights, inputs.T)
# displays the following result:
# array([[-0.01299],# 
#        [ 0.00664],
#        [ 0.13494]])


Out[71]:
array([[-0.01299],
       [ 0.00664],
       [ 0.13494]])

In [ ]: